<template>
  <el-container class="homeTop">
    <!-- 头部 -->
    <el-header>
        <div>
            <img src="../assets/heima.png" alt="">
            <span>后台管理</span>
        </div>
        <el-button type="info" @click="loginExit">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边导航菜单部位 -->
      <el-aside :width="collapse?'65px':'200px'">
        <div @click="collpseMenu" class="toggle-collapse">|||</div>
        <el-menu background-color="#373d41" router :collapse="collapse" :default-active='activePath' text-color="#fff" :collapse-transition="false" unique-opened active-text-color="#2b70d9">
      <!-- 一级菜单 -->
      <el-submenu :index="subItem.id+''" v-for="subItem in menuList" :key="subItem.id">
        <!-- 一级菜单模板 -->
        <template slot="title">
          <!-- 图标 -->
          <i :class="subItemIcon[subItem.id]"></i>
          <!-- 文本 -->
          <span>{{subItem.authName}}</span>
        </template>

        <!-- 二级菜单 -->
        <el-menu-item :index="'/'+item.path" v-for="item in subItem.children" :key="item.id" @click="savNavStatus('/'+item.path)">
              <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{item.authName}}</span>
              </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data: function () {
    return {
      menuList: [],
      subItemIcon: {
        125: 'iconfont icon-yonghu',
        103: 'iconfont icon-zhankaicebianlan',
        101: 'iconfont icon-zhankaicebianlan',
        102: 'iconfont icon-zhankaicebianlan',
        145: 'iconfont icon-mima'
      },
      collapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMeauList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录
    loginExit () {
      window.sessionStorage.clear()
      this.$router.push('login')
    },
    // 获取侧边菜单信息
    async getMeauList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$massage.error('res.meta.msg')
      this.menuList = res.data
    },
    // 控制折叠
    collpseMenu () {
      this.collapse = !this.collapse
    },
    // 高亮选择的子菜单
    savNavStatus (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>

<style lang="less" scoped>
header{
    background-color: #373d41;
    display: flex;
    justify-content:space-between ;
    align-items: center;
    color: #ffff;
    > div {
      display: flex;
      align-items: center;
      font-size: 24px;
      > span {
        margin-left: 15px;
      }
    }
    }
// 侧边菜单
aside{background-color: #333744;
  .el-menu{
    border-right: 0;
  }
}
.homeTop{height: 100%;}
.iconfont{
  margin-right: 10px;
}
// 折叠按钮
.toggle-collapse{
  color: #ffff;
  font-size: 10px;
  text-align: center;
  line-height: 20px;
}
</style>
